﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DbLibs;

namespace MySQLHelper
{
    public partial class PojoGen : Form
    {

        SqlConn db = new SqlConn();

        string SQL = "";

        DataView dvTmp = new DataView();


        public PojoGen()
        {
            InitializeComponent();
        }

        private void PojoGen_Load(object sender, EventArgs e)
        {
            cmbLang.SelectedIndex = 0; 
            
            //db = new DbLibs.ClsDbase();
            SQL = "show tables";

            DataTable dtTmp = new DataTable("data");

            dtTmp = db.execQuery(SQL);
            dtTmp.TableName = "data";

            dvTmp.Table = dtTmp;

            gv.DataSource = dvTmp;
            //db = null;

        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            //db = new DbLibs.ClsDbase();
            string tableName = gv.CurrentRow.Cells[0].Value.ToString();
            SQL = String.Format("SELECT * FROM {0} WHERE ?1=?2 LIMIT 0,1", tableName);
            DataTable dt = db.execQuery(SQL, new string[] { "?1", "?2" }, new object[] { 1, 1 });
            //db = null;

            tableName = this.ConvClassName(tableName); 

            if(cmbLang.Text=="VB")
                txt.Text  = this.GenVBLang(tableName, dt);
            else
                txt.Text = this.GenNewCSharpLang(tableName, dt);



        }

        private string GenCSharpLang(string tableName, DataTable dt)
        {
            //generate class
            StringBuilder sb = new StringBuilder();
            string colName = "";

            sb.AppendLine(String.Format("public class {0}", tableName));
            sb.AppendLine("{");

            foreach (DataColumn col in dt.Columns)
            {
                colName = this.ConvClassName(col.ColumnName);

                if (col.DataType == typeof(System.String))
                {
                    sb.AppendLine(String.Format("public string {0} {1}", colName, "{ set; get; }"));
                }
                else if (col.DataType == typeof(System.Int16))
                {
                    sb.AppendLine(String.Format("public Int16 {0} {1}", colName, "{ set; get; }"));
                }
                else if (col.DataType == typeof(System.Int32))
                {
                    sb.AppendLine(String.Format("public Int32 {0} {1}", colName, "{ set; get; }"));
                }
                else if (col.DataType == typeof(System.Int64))
                {
                    sb.AppendLine(String.Format("public Int64 {0} {1}", colName, "{ set; get; }"));
                }
                else if (col.DataType == typeof(System.Double))
                {
                    sb.AppendLine(String.Format("public Double {0} {1}", colName, "{ set; get; }"));
                }
                else if (col.DataType == typeof(System.Decimal))
                {
                    sb.AppendLine(String.Format("public Decimal {0} {1}", colName, "{ set; get; }"));
                }
                else if (col.DataType == typeof(System.DateTime))
                {
                    sb.AppendLine(String.Format("public DateTime {0} {1}", colName, "{ set; get; }"));
                }
                else if (col.DataType == typeof(System.Boolean))
                {
                    sb.AppendLine(String.Format("public Boolean {0} {1}", colName, "{ set; get; }"));
                }

                sb.AppendLine("");

            }

            sb.AppendLine("}");

            return sb.ToString(); 
            
        }

        private string GenNewCSharpLang(string tableName, DataTable dt)
        {
            //generate class
            StringBuilder sb = new StringBuilder();
            string colName = "";

            sb.AppendLine(String.Format("public class {0}", tableName));
            sb.AppendLine("{");

            foreach (DataColumn col in dt.Columns)
            {

                sb.AppendLine(String.Format("public static string[] {0} = new string[]{3} \"{1}\", \"{2}\" {4};", col.ColumnName.ToUpper(),
                                                                                                          col.ColumnName,
                                                                                                          col.ColumnName,"{","}"));

                colName = this.ConvClassName(col.ColumnName);

                if (col.DataType == typeof(System.String))
                {
                    sb.AppendLine(String.Format("public string {0};", colName));
                }
                else if (col.DataType == typeof(System.Int16))
                {
                    sb.AppendLine(String.Format("public Int16 {0};", colName));
                }
                else if (col.DataType == typeof(System.Int32))
                {
                    sb.AppendLine(String.Format("public Int32 {0};", colName));
                }
                else if (col.DataType == typeof(System.Int64))
                {
                    sb.AppendLine(String.Format("public Int64 {0};", colName));
                }
                else if (col.DataType == typeof(System.Double))
                {
                    sb.AppendLine(String.Format("public Double {0};", colName));
                }
                else if (col.DataType == typeof(System.Decimal))
                {
                    sb.AppendLine(String.Format("public Decimal {0};", colName));
                }
                else if (col.DataType == typeof(System.DateTime))
                {
                    sb.AppendLine(String.Format("public DateTime {0};", colName));
                }
                else if (col.DataType == typeof(System.Boolean))
                {
                    sb.AppendLine(String.Format("public Boolean {0};", colName));
                }

                sb.AppendLine("");

            }

            sb.AppendLine("}");

            return sb.ToString();

        }


        private string GenVBLang(string tableName, DataTable dt)
        {
            //generate class
            StringBuilder sb = new StringBuilder();
            string colName = "";

            sb.AppendLine(String.Format("public class {0}", tableName));
            sb.AppendLine("");

            foreach (DataColumn col in dt.Columns)
            {
                colName = this.ConvClassName(col.ColumnName);

                if (col.DataType == typeof(System.String))
                {
                    sb.AppendLine(String.Format("public Property {0} As String", colName));
                }
                else if (col.DataType == typeof(System.Int16))
                {
                    sb.AppendLine(String.Format("public Property {0} As Int16", colName));
                }
                else if (col.DataType == typeof(System.Int32))
                {
                    sb.AppendLine(String.Format("public Property {0} As Int32", colName));
                }
                else if (col.DataType == typeof(System.Int64))
                {
                    sb.AppendLine(String.Format("public Property {0} As Int64", colName));
                }
                else if (col.DataType == typeof(System.Double))
                {
                    sb.AppendLine(String.Format("public Property {0} As Double", colName));
                }
                else if (col.DataType == typeof(System.Decimal))
                {
                    sb.AppendLine(String.Format("public Property {0} As Decimal", colName));
                }
                else if (col.DataType == typeof(System.DateTime))
                {
                    sb.AppendLine(String.Format("public Property {0} As DateTime", colName));
                }
                else if (col.DataType == typeof(System.Boolean))
                {
                    sb.AppendLine(String.Format("public Property {0} As Boolean", colName));
                }

                sb.AppendLine("");

            }

            sb.AppendLine("End Class");

            return sb.ToString();

        }


        string ConvClassName(string str)
        {
            StringBuilder sb = new StringBuilder();

            string[] tmp = str.Split('_');

            foreach (string s in tmp)
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (i == 0)
                    {
                        sb.Append(s[i].ToString().ToUpper());
                    }
                    else
                    {
                        sb.Append(s[i].ToString().ToLower());
                    }
                }
            }

            return sb.ToString();
        }

        private void txtFilter_TextChanged(object sender, EventArgs e)
        {
            dvTmp.RowFilter = String.Format("name LIKE '%{0}%'", txtFilter.Text.Trim());  
        }
    }
}
